package de.dopichaj.labrador.search;

import java.io.File;
import java.util.Collection;

import org.apache.log4j.Logger;

import de.dopichaj.labrador.search.hit.DocHitMap;
import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.search.hit.HitHierarchyTree;
import de.dopichaj.labrador.search.prune.ResultTreePruner;


/**
 * Used for merging and ignoring markup.
 * @author Benedikt
 */
public class XMLSearchPostProcessor {

    /**
     * A logger for this class
     */
    private static final Logger log = Logger.getLogger(XMLSearchPostProcessor.class);
    
    private final ResultTreePruner pruner;
        
    /**
     * Initializes the XMLSearchPostProcessor
     */
    public XMLSearchPostProcessor(ResultTreePruner pruner) {
        this.pruner = pruner;
    }
    
    /**
     * Merge a complete set of hits.
     * @param results the results to merge (will be modified).
     */
    public void prune(DocHitMap results) {
        log.info("Starting pruning");
        for (File file : results.getDocs()) {
            Collection<Hit> hits = results.getHitsFromDoc(file);
            results.setHitsFromDoc(file, pruneHits(hits));
        }
        log.info("Finished pruning");
    }
    
    /**
     * Performs result merging.
     * @param hits The results of a de.dopichaj.labrador.search.
     * @return Merged results.
     */
    public Collection<Hit> pruneHits(Collection<Hit> hits) {
        if(log.isDebugEnabled()) {
            log.debug("Pruning hits: " + hits.size() + " results before merging.");
        }
                
        HitHierarchyTree hitHierarchy = new HitHierarchyTree(hits);

        log.debug("HitHierarchy created.");
        
        pruner.prune(hitHierarchy);

        Collection<Hit> result =  hitHierarchy.generateHitList();
        if(log.isDebugEnabled()) {
            log.debug("Finished merging hits: " + result.size() + " results left.");
        }

        return result;
    }
}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
